package com.ftg.shopping.userGroupPrivilege.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.shopping.config.LayuiPage;
import com.ftg.shopping.sp_deptinfo.entity.Sp_deptinfo;
import com.ftg.shopping.sp_deptinfo.mapper.Sp_deptinfoMapper;
import com.ftg.shopping.userGroupPrivilege.entity.sp_roleinfo;
import com.ftg.shopping.userGroupPrivilege.mapper.UserGroupPrivilegeMapper;
import com.ftg.shopping.userGroupPrivilege.mapper.userrole_midmapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class UserGroupPrivilegeService {

    @Autowired
    UserGroupPrivilegeMapper userGroupPrivilegeMapper;
    @Autowired
    userrole_midmapper userrole_midmapper;

    public LayuiPage<Sp_deptinfo> selectall(int page, int limit, String search){
        int page2 =(page - 1)*limit;
        LayuiPage dept = new LayuiPage<>();
        if (search == null){
            dept.setData(userGroupPrivilegeMapper.selectall(page2,limit));
            dept.setCount(userGroupPrivilegeMapper.selectall2().size());
        }else {
            dept.setData(userGroupPrivilegeMapper.selectall3(page2,limit,search));
            dept.setCount(userGroupPrivilegeMapper.selectall4(search).size());
        }

        return dept;
    }

    /**
     * 显示分组
     * @param userid
     * @return
     */
    public LayuiPage<sp_roleinfo> role2(String userid) {

        LayuiPage roleinfo = new LayuiPage<>();
        roleinfo.setData(userGroupPrivilegeMapper.role2(userid));
        return roleinfo;
    }


    /**
     * 分组点击按钮
     * @param userid
     * @param roleId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int roleinfo(int userid, String roleId) {

        String[] cla = roleId.split("p");
        userrole_midmapper.deleteById(userid);
        for (int i = 0;i < cla.length;i++){
            userGroupPrivilegeMapper.insertmore(userid,cla[i]);
        }

        return  1;
    }


//    @Transactional(rollbackFor = Exception.class)
//    public int roleinfo(int userid, String roleId) {
//
//        String[] cla = roleId.split(" ");
//        userGroupPrivilegeMapper.deleteById(userid);
//        for (int i = 0;i <= cla.length;i++){
//            userGroupPrivilegeMapper.insertmore(userid,cla[i]);
//        }
//
//        return  1;
//    }




}
